/* ======================================  路由跳转  ====================================== */
import { useRoute, useRouter } from 'vue-router'

const Index = {
	// ========================================================================================= 框架跳转
	route(){ return useRoute() },
	
	router(){ return useRouter() },
	
	push(path, query, router){
		const $ = router ? router : useRouter()
		$.push( query ? { path, query } : path )
	},
	replace(){
		const $ = router ? router : useRouter()
		$.replace( query ? { path, query } : path )
	},
	back(){
		window.history.back( )
	},
	query(){ return this.route().query },
	existMain(rootPath,value){ return (new RegExp(`^${rootPath}/.*`)).test( value ) },
	existHttp: value => /(http|https):\/\/\S*/.test( value ),
	// ========================================================================================= 动态添加路由
	deepRouter(data, child, rootPath, hasFolder){
		const modules = [
			import.meta.glob('../../../views/*.vue'),
			import.meta.glob('../../../views/*/*.vue'),
			import.meta.glob('../../../views/*/*/*.vue'),
			import.meta.glob('../../../views/*/*/*/*.vue'),
			import.meta.glob('../../../views/*/*/*/*.vue')
		]	
		let router = []
		
		const deep = (data, path) =>{
			data.forEach( v =>{
				let { children, name, url, meta, component } = v
				
				if( url ){
					if( this.existHttp(url) ){
						v.path = url
					}else{
						v.path = this.existMain(rootPath,url) ? url : rootPath + url
					}
				}else{
					if(v.path){
						if( this.existHttp(v.path) ){
							v.path = url
						}else{
							if(!this.existMain(rootPath,v.path)){ v.path = rootPath + v.path }
						}
					}else{
						if( name ){ v.path = path + '/' + name }
					}
				}
				if($fn.hasArray(children)){
					deep(children, v.path)
				}else{
					let uri = component ? component : v.path
					if( uri ){
						uri = hasFolder === false ? uri.replace(rootPath,'') : uri
						const index = uri.split('/').length - 2
						try{
							router.push({ path:v.path, meta, component: modules[index][`../../../views${uri}.vue`] })
						}catch(e){
							console.error( uri + +'.vue 不存在'  )
						}
					}
				}
			})
		}
		
		deep(data, rootPath)
		
		child.forEach(({ path, component, meta })=>{
			let url = component ? component : path
			url = url.replace(rootPath,'')
			const index = url.split('/').length - 2
			router.push({ path, component: modules[index][`../../../views${url}.vue`] })
		})
		
		router.push({ path: rootPath + '/:pathMatch(.*)*', component: () => import('../../../views/404.vue') })
		return { router, menuData:data }
	},
	async getDymaicMenu({childRouter,isLogin, api, data, rootPath='/main', hasFolder=true}){
		let menu = $fn.session('menu')
		if( isLogin || !$fn.hasArray(menu)){
			if( $fn.hasArray( data )){ menu = data }
			if( ($fn.getToken() || isLogin) && api){ menu = await window.$http.pull(null, api, { loading:true, pass:true }) }
			menu = $fn.hasArray( menu ) ? menu : []
		}
		
		const { router, menuData } = this.deepRouter(menu, childRouter, rootPath, hasFolder)
		
		if( $fn.hasArray( menuData )){ $fn.session('menu', menuData) }
		
		window.router.addRoute({
			path: rootPath, 
			component: () => import('@views/index.vue'),
			children: router
		})
		return true
	}
}

export default Index